package ordering.dyn;

import ordering.VariableOrdering;
import general.CPA.CPA;
import general.CPA.DynamicCPA;
import general.conflictset.ConflictSet;
import general.constraints.VariableConstraints;

public abstract class DynamicOrdering implements VariableOrdering{

	@Override
	public byte[] between(CPA p, byte low, byte high) {
		if (p instanceof DynamicCPA){
			return between((DynamicCPA)p, low, high);
		} else {
			new Exception("Cannot perform dynamic ordering with a static CPA").printStackTrace();
			return null;
		}
	}

	private byte[] between(DynamicCPA p, byte low, byte high){
		return p.between(low, high);
	}

	private void removeBigger(DynamicCPA cpa, ConflictSet conflicts, byte id) {
		cpa.removeBigger(conflicts, id);
	}

	private byte previous(DynamicCPA cpa, byte id) {
		return cpa.previous(id);
	}

	private void removeBigger(DynamicCPA cpa, byte h) {
		cpa.removeBigger(h);
	}

	public abstract byte next(DynamicCPA p, byte agent, VariableConstraints c);

	public byte max(DynamicCPA p, byte agent, ConflictSet conflicts) {
		return p.max(agent, conflicts);
	}
	
	public boolean isBefore(DynamicCPA p, byte cpaCreator, byte maxConflicted) {
		return p.isBefore(cpaCreator, maxConflicted);
	}
	
	@Override
	public boolean isBefore(CPA p, byte cpaCreator, byte maxConflicted) {
		if (p instanceof DynamicCPA){
			return isBefore((DynamicCPA)p, cpaCreator, maxConflicted);
		} else {
			new Exception("Cannot perform dynamic ordering with a static CPA").printStackTrace();
			return false;
		}
	}
	
	@Override
	public byte next(CPA p, byte agent, VariableConstraints c) {
		if (p instanceof DynamicCPA){
			return next((DynamicCPA)p, agent, c);
		} else {
			new Exception("Cannot perform dynamic ordering with a static CPA").printStackTrace();
			return -1;
		}
	}

	@Override
	public byte max(CPA p, byte agent, ConflictSet c) {
		if (p instanceof DynamicCPA){
			return max((DynamicCPA)p, agent, c);
		} else {
			new Exception("Cannot perform dynamic ordering with a static CPA").printStackTrace();
			return -1;
		}
	}

	@Override
	public byte previous(CPA p, byte agent) {
		if (p instanceof DynamicCPA){
			return previous((DynamicCPA)p, agent);
		} else {
			new Exception("Cannot perform dynamic ordering with a static CPA").printStackTrace();
			return -1;
		}
	}

	@Override
	public void removeBigger(CPA cpa, ConflictSet conflicts, byte id) {
		if (cpa instanceof DynamicCPA){
			removeBigger((DynamicCPA)cpa, conflicts, id);
		} else {
			new Exception("Cannot perform dynamic ordering with a static CPA").printStackTrace();
		}
	}

	@Override
	public void removeBigger(CPA cpa, byte h) {
		if (cpa instanceof DynamicCPA){
			removeBigger((DynamicCPA)cpa, h);
		} else {
			new Exception("Cannot perform dynamic ordering with a static CPA").printStackTrace();
		}
	}

}
